//
//  ZHMineSetViewController.m
//  LoveFreshBee
//
//  Created by brice Mac on 2016/10/16.
//  Copyright © 2016年 王忠迪. All rights reserved.
//

#import "HMSystemCell.h"
#import "Masonry.h"

@interface HMSystemCell ()

@property(nonatomic,weak)UIView *separatorView;

@end

@implementation HMSystemCell

-(UILabel *)leftLable
{
    if (!_leftLable) {
        _leftLable = [[UILabel alloc]init];
        _leftLable.font = [UIFont systemFontOfSize:16];
    }
    return _leftLable;
}

-(UIButton *)rightBtn
{
    if (!_rightBtn) {
        _rightBtn = [[UIButton alloc]init];
        [_rightBtn.titleLabel setFont: [UIFont systemFontOfSize:15]];
        
        [_rightBtn addTarget:self action:@selector(rightBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _rightBtn;
}

-(UILabel *)lable {
    if (!_lable) {
        _lable = [[UILabel alloc]init];
        _lable.numberOfLines = 0;
        _lable.textColor = [UIColor grayColor];
        _lable.font = [UIFont systemFontOfSize:13];
        _lable.preferredMaxLayoutWidth = kScreenW - 2 * 10;
        
    }
    return _lable;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    [self.contentView addSubview:self.leftLable];
    
    [self.contentView addSubview:self.rightBtn];
    
    //设置cell 点击不变颜色
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    
    [_leftLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        
        make.left.equalTo(self).offset(10);
        
        make.height.mas_equalTo(30);

    }];
    
    [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        
        make.right.equalTo(self).offset(-10);
        
        make.height.mas_equalTo(30);
        
    }];
    
    UIView *separatorView = [[UIView alloc]initWithFrame:CGRectMake(0, 59.5, kScreenW, 1)];
    separatorView.backgroundColor = [UIColor blackColor];
    separatorView.alpha = 0.15;
    self.separatorView = separatorView;
    [self.contentView addSubview:separatorView];
    
    
    [self.contentView addSubview:self.lable];
    
    [_lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(separatorView).offset(10);
        make.left.equalTo(self).offset(10);
        
    }];
    
}


- (void)setModel:(HMModel *)model
{
    _model = model;
    
    self.leftLable.text = model.title;
    
    self.rightBtn.selected = model.isShow;
    
    
    [self.rightBtn setTitle:@"显示全部" forState:UIControlStateNormal];
    [self.rightBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    
    [self.rightBtn setTitle:@"折叠显示" forState:UIControlStateSelected];
    [self.rightBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
    
    _lable.text = model.content;
    
    self.contentText = model.content;
    
    if (_model.isShow == NO) {
        
        _model.rowHeight = 120;
        
        [_lable updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(32);
        }];
        [_lable layoutIfNeeded];
        
    }else {
        
        _lable.text = _model.content;
        [self.contentView layoutIfNeeded];
        
        
        _model.rowHeight = CGRectGetMaxY(self.lable.frame) + 20;
        
        [_lable updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).offset(-10);
        }];
        
        [self.lable layoutIfNeeded];
    }
    
}

//CGFloat textWidth = [self.model.title sizeWithAttributes:@{NSFontAttributeName:self.titleLabel1.font}].width;


- (void)rightBtnDidClick:(UIButton *)sender {
    
    _rightBtn.selected = !sender.isSelected;
    
    self.model.isShow = _rightBtn.isSelected;
    
    if (self.model.isShow == 1) {
        
//        self.lable.frame = CGRectMake(10, 10, kScreenW - 2*10, 80);
        
        self.model.content = self.contentText;
    } else {
        
        self.model.content = self.contentText;
    }
    
    
    if (self.rightBtnCallBack) {
        
        self.rightBtnCallBack(_lable);
    }
    
}




@end
